/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 $(document).ready(function(){
   
   //Para dibujar triangulo
   var sqrt3=Math.sqrt(3);
   
   var l=$('#tateti-g').width()-40;
   var x=$('#tateti-g').width()/2;
   var y=$('#tateti-g').height()*0.09;
   var radio=15;
   
   var cont=0;
   var Moves = new Array(9);
   var stackMovJ1 = new Array();
   var stackMovJ2 = new Array();
   
   var winMoves=new Array(9);
    
   winMoves[0]= new Array(0,1,2);
   winMoves[1]= new Array(0,7,3);
   winMoves[2]= new Array(0,5,4);
   winMoves[3]= new Array(1,6,4);
   winMoves[4]= new Array(1,7,8);
   winMoves[5]= new Array(2,3,4);
   winMoves[6]= new Array(2,8,5);
   winMoves[7]= new Array(3,8,6);
   winMoves[8]= new Array(5,6,7);
   
   function findPosMov(mv){
     var pMovs=new Array();
     for(i=0;i<9;i++){
       for(j=0;j<3;j++)
         if (mv==winMoves[i][j]) 
           {
             pMovs.push(i);
             console.log(i);
             break;
           }
     }
     return pMovs;
   }
   
   function cpyArray(arraySrc,arrayDst){
     console.log(arraySrc.length);
     for(i=0;i<arraySrc.length;i++){
       
       arrayDst[i]=arraySrc[i];
     }  
   }
   
   function find(ob,array){
     for(var i in array){
       if(ob==array[i])
         return true;
     }
    return false;
     
   }
   
   function verifyWin(mov,stackTmp){
     
    var pM= new Array();
    cpyArray(findPosMov(mov),pM);
    var cont=0;
     
    console.log('Verificando si jugador gana la partida');
    
    for(i=0;i<pM.length;i++){
      
      cont=0;
      for(j=0;j<stackTmp.length;j++){ 
 
        
        if(find(stackTmp[j],winMoves[pM[i]]) && cont!=3 ){
             cont++;
             console.log('Concuerda en:'+cont);
        }
        if(cont==3) return true;
      }
         
     }
     
    return false;   
   }
      
   Moves[0] = (new point(x,y,0));
   Moves[1] = (new point(x-l/6,y+l*sqrt3/6,1));
   Moves[2] = (new point(x-l/2,y+l*sqrt3/2,2));
   Moves[3] = (new point(x-l/6,y+l*sqrt3/2,3));
   Moves[4] = (new point(x+l/2,y+l*sqrt3/2,4));
   Moves[5] = (new point(x+l/3,y+l*sqrt3/3,5));
   Moves[6] = (new point(x+0.25*l/2,y+0.63*l*sqrt3/2,6));
   Moves[7] = (new point(x-0.2*l/2,y+0.57*l*sqrt3/2,7));
   Moves[8] = (new point(x-0.05*l/2,y+0.8077*l*sqrt3/2,8));
   
   var g=$g('tateti-g');
   
   g.add(function(ctx){
     ctx.strokeStyle = '#9966ff';
     ctx.lineWidth=5;
     ctx.beginPath();
     
     //Dibujando triangulo
     ctx.moveTo(Moves[0].getX(),Moves[0].getY());
     ctx.lineTo(Moves[2].getX(),Moves[2].getY());
     ctx.lineTo(Moves[4].getX(),Moves[4].getY());
     ctx.lineTo(Moves[0].getX(),Moves[0].getY());
     
     //Dibujando lineas interiores
     ctx.moveTo(Moves[0].getX(),Moves[0].getY());
     ctx.lineTo(Moves[3].getX(),Moves[3].getY());
     
     ctx.moveTo(Moves[2].getX(),Moves[2].getY());
     ctx.lineTo(Moves[5].getX(),Moves[5].getY());
     
     ctx.moveTo(Moves[4].getX(),Moves[4].getY());
     ctx.lineTo(Moves[1].getX(),Moves[1].getY());
     
     ctx.moveTo(Moves[1].getX(),Moves[1].getY());
     ctx.lineTo(Moves[8].getX(),Moves[8].getY());
     
     ctx.moveTo(Moves[5].getX(),Moves[5].getY());
     ctx.lineTo(Moves[7].getX(),Moves[7].getY());
     
     ctx.moveTo(Moves[3].getX(),Moves[3].getY());
     ctx.lineTo(Moves[6].getX(),Moves[6].getY());
     
     ctx.stroke(); 
     
     
   });
   
   //Dibujando circulos en cada punto
   
   
   for(i in Moves){
       g.add('target', new Circulos(Moves[i].getX(), Moves[i].getY(),radio,Moves[i].getID(),{color:'#8f3299'}));
   }
   
   g.mousemove('target',function(){})
   .mouseenter('target',function(e){this.toggleHover();})
   .mouseleave('target',function(e){this.toggleHover();});
   
   g.click('target',function(e){ 
     if(this.active) 
	    alert('Movida ya realizada');
	   
     else{
       cont++;
	     this.setActive();
	     
       if(cont%2==0){
         this.setColor('J2');
         stackMovJ2.push(this.id);
         if(stackMovJ2.length>=3 ){ 
           if(verifyWin(this.id,stackMovJ2)){ 
             console.log('Jugador 2 gano la partida. Juego finalizado!.');
             $('#overlay').fadeIn('fast',function(e){
               $('#message').append('Jugador 2 Gana la partida');
               $('#boxRes').animate({'top':'160px'},500);
             });
           }
         }
       }
       else{
         this.setColor('J1');
         stackMovJ1.push(this.id);
         if(stackMovJ1.length>=3){ 
           if(verifyWin(this.id,stackMovJ1)){ 
             console.log('Jugador 1 gano la partida. Juego finalizado!.');
             $('#overlay').fadeIn('fast',function(){
               $('#message').append('Jugador 1 Gana la partida');
               $('#boxRes').animate({'top':'160px'},500);
             });
          }
         }
         
       }
       if(cont==9) {
        $('#overlay').fadeIn('fast',function(){
             $('#message').append('Juego empatado');
             $('#boxRes').animate({'top':'160px'},500);
        }); 
       }   
      
      $('#Movidas').append('<li> Movimiento realizado en el nodo con ID:'+ this.id +' </li>');
      
      $.post('tateti',{ id: this.id },function(data){
          alert('Success: '+data.success +'\n'+
                'Mensaje: ' +data.mensaje +'\n'+
                'Movida ID:' + data.param);
      },'json');
      
       
     }
     
   });
       
   g.play(5).draw(); 
 });

